(function () {

  layui.use(['jquery', 'form'], function () {
    var $ = null;
    var form = null;
    var loging = false;
    $ = layui.$;
    form = layui.form;

    btgo.getRsa($);
    bindEvent(); // 绑定事件

    // 绑定事件
    function bindEvent() {
      // 登录提交
      form.on('submit(login)', function (data) {
        var formdata = data.field;
        var elem = data.elem;
        formdata.userid = $(this).attr('data-userid');
        // !loging && login(formdata, elem);
        login(formdata, elem);
      });

      // 回车键
      btgo.enter($, {
        el: '.form-container',
        callback: function callback() {
          $('.login-btn').click();
        }
      });
    }

    // 登录
    function login(form, elem) {
      // elem.innerText = '登录中...';
      loging = true;
      var url = '?r=userlogin/loginByPhone';
      // 获取登录时间
      btgo.request($, {
        url: url,
        data: {
          action: 'time'
        },
        success: function success(res) {
          // 登录
          if (res.status == 200) {
            var data = btgo.encrypt({
              phone: document.getElementById("phone").value,
              smscode: document.getElementById("smscode").value,
              time: res.data.time,
              userid: form.userid
            });
            btgo.request($, {
              url: url,
              type: 'post',
              data: {
                loginencrypt: data,
              },
              success: function success(res) {
                console.log(res);
                if (res.status == 200) {
                  if(res.data.account_list){
                    selectAccount(res.data.account_list);
                    return true;
                  }else{
                    window.location.href = res.data.url;
                  }
                }
                if (res.vaildcodeshow == 1) {
                  $('.code-row').addClass('layui-show');
                }
                loging = false;
                elem.innerText = '登录';
                btgo.msg(res.message);
              },
              error: function error(res) {
                console.log(res);
                btgo.msg('服务器异常，请重试');
                elem.innerText = '登录';
                loging = false;
              }
            });
          }
        }
      });
    }

    function selectAccount(data){
      var html = '<table class="layui-table" lay-skin="nob"><tbody>';
      for (var i=0; i<data.length; i++){
        html += '<tr><td>'+data[i].username+'</td><td>'+data[i].channel_name+'</td><td><button type="button" class="layui-btn layui-btn-sm layui-btn-normal" data-userid="'+data[i].userid+'" lay-submit lay-filter="login">进入</button></td></tr>';
      }
      html += '</tr></tbody></table>';
      layui.use(['layer'], function () {
        layer.open({
          type: 1,
          title: '请选择登录账号'
          ,area: ['400px', '300px']
          ,content: html
        });
      });
    }

    let time = 60;
    let timer = false
    let falg = false
    //发送验证码
    $('#send_smscode').click(function(){
      var phone = $("#phone").val();
      if(phone == ''){
        alert('请输入手机号');
        return false;
      }

      if(falg) return
      falg = true
      timer = setInterval(() => {
        time--
        $('#send_smscode').attr('value', time)
        $('#send_smscode').text(time)
        $('#send_smscode').addClass('noClick')
        if (time == 0) {
          clearInterval(timer)
          time = 60;
          $('#send_smscode').attr('value', '发送')
          $('#send_smscode').text('发送')
          $('#send_smscode').removeClass('noClick')
          falg = false
        }
      }, 1000)

      $.post("?r=userlogin/loginByPhone", {
        action: "time"
      }, function (data) {
        if (data.status == 200) {
          // $('#send_smscode').hide()
          // $('.verifcode').show()
          var res = btgo.encrypt({
            phone: phone,
            time: data.data.time,
          });
          $.ajax({
            type: "post",
            url: '?r=userlogin/sendSMS',
            data: {
              loginencrypt: res,
            },
            dataType: "json",
            success: function (data) {
              if(data.status != 200){
                alert(data.msg);
              }
            }
          });
        } else {
          falg = false
          time = 60;
          $('#send_smscode').removeClass('noClick');
          $('#send_smscode').attr('value', '发送');
          $('#send_smscode').text('发送');
          alert("获取信息异常");
        }
      }, "json");
    })
  });
})();